package com.perceptivesoftware.licensing.client.dialogs.useradmin.plugin.eh;

import java.util.ArrayList;
import java.util.List;
import com.perceptivesoftware.licensing.client.LicensingRPCFactory;
import com.perceptivesoftware.licensing.client.PerceptiveUISession;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.SecurityManagerView;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.callbackhandlers.AddCallbackHandler;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.callbackhandlers.ItemSelectedCallbackHandler;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.callbackhandlers.SecurityManagerAsyncCallback;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.interfaces.Addable;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.interfaces.Parent;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.plugin.SecurityManagerNavItem;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.plugin.SecurityManagerPlugin;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.plugin.role.RoleNavItem;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.plugin.superadmin.SuperAdminRootNavItem;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.plugin.user.UserNavItem;

public class EHPlugin extends SecurityManagerPlugin<EHNavItem> implements Parent, Addable {

	private EHView ehView;

	@Override
	public void init() {
		ehView = new EHView();
	}

	@Override
	public String getIconStyle() {
		return "nav-icon-root";
	}

	@Override
	public SecurityManagerView getView() {
		return ehView;
	}

	@Override
	protected void itemSelected(@SuppressWarnings("unused") final EHNavItem ani, final ItemSelectedCallbackHandler isch) {
		isch.doneRendering();
	}

	@Override
	public List<SecurityManagerNavItem> getChildren(@SuppressWarnings("unused") final SecurityManagerNavItem parent) {
		// final OrgNavItem org = (OrgNavItem) parent;
		final ArrayList<SecurityManagerNavItem> returnroles = new ArrayList<SecurityManagerNavItem>();
		for (final RoleNavItem roleNavItem : PerceptiveUISession.getInstance().getInitialRoles()) {
			returnroles.add(roleNavItem);
		}
		returnroles.add(new SuperAdminRootNavItem());
		return returnroles;
	}

	@Override
	public void add(final String name, @SuppressWarnings("unused") final SecurityManagerNavItem parent, final AddCallbackHandler ach) {
		LicensingRPCFactory.getSecurityService(true).createRole(name, new SecurityManagerAsyncCallback<RoleNavItem>() {

			@Override
			public void onSuccess(final RoleNavItem roleNavItem) {
				if (roleNavItem == null) {
					ach.addFailed("Does another role already exist with this name?");
				}
				else {
					PerceptiveUISession.getInstance().getInitialRoles().add(roleNavItem);
					ach.added(roleNavItem);
				}
			}
		});
	}

	@Override
	public String getAddIconStyle() {
		return "nav-icon-role-add";
	}

	@Override
	public String getAddName() {
		return "Role";
	}

	public String getDeleteIconStyle() {
		return "nav-icon-role-delete";
	}

	public String getRenameIconStyle() {
		return "nav-icon-role-rename";
	}

	@Override
	public Boolean canAdd(@SuppressWarnings("unused") final SecurityManagerNavItem parent) {
		return PerceptiveUISession.getInstance().isSuperAdmin();
	}

	public Boolean canDelete(@SuppressWarnings("unused") final SecurityManagerNavItem ani) {
		return PerceptiveUISession.getInstance().isSuperAdmin();
	}

	public Boolean canRename(@SuppressWarnings("unused") final SecurityManagerNavItem ani) {
		return PerceptiveUISession.getInstance().isSuperAdmin();
	}
}
